package com.igirl.manage.controller;

import com.igirl.manage.pojo.Item;
import com.igirl.manage.pojo.ItemCat;
import com.igirl.manage.pojo.ItemDesc;
import com.igirl.manage.service.item.ItemCatService;
import com.igirl.manage.service.item.ItemDescService;
import com.igirl.manage.service.item.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: jiaheng
 * @ Description:
 * @ Date: Created in 15:09 2017/12/26
 * @ Modified:
 */
@RequestMapping("item")
@Controller
public class ItemController {

    @Autowired
    private ItemService itemService;

    @Autowired
    private ItemCatService itemCatService;

    @Autowired
    private ItemDescService itemDescService;


    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Item> saveItem(Item item, @RequestParam("desc") String desc) {
        if (item == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        try {
            item.setStatus(0);
            this.itemService.save(item);
            Long id = item.getId();
            ItemDesc itemDesc = new ItemDesc();
            itemDesc.setItemId(Long.valueOf(id));
            itemDesc.setItemDesc(desc);
            itemService.saveItemDesc(itemDesc);
            return ResponseEntity.status(HttpStatus.CREATED).body(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<Item> updateItem(Item item, @RequestParam("desc") String desc) {
        if (item == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        try {
            item.setStatus(0);
            this.itemService.updateSelective(item);
            Long id = item.getId();
            ItemDesc itemDesc = new ItemDesc();
            itemDesc.setItemId(Long.valueOf(id));
            itemDesc.setItemDesc(desc);
            itemService.updateItemDesc(itemDesc);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @RequestMapping("cat")
    public ResponseEntity<List<ItemCat>> queryItemCat(@RequestParam(value = "id",defaultValue = "0")
                                                              Long id) {
        try {
            ItemCat itemCat = new ItemCat();
            itemCat.setParentId(id);
            List<ItemCat> itemCatList = this.itemCatService.queryListByWhere(itemCat);
            if (itemCatList == null || itemCatList.isEmpty()) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(itemCatList);
        } catch (Exception e) {
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<List<Item>> queryItemByPage(@RequestParam("page") Integer page,
                                                      @RequestParam("rows") Integer rows) {
        if (page == null || rows == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        try {
            String order = "id desc";
            List<Item> itemList = this.itemService.queryAll(order,page, rows);
            return ResponseEntity.ok(itemList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @RequestMapping(value = "desc/{id}",method = RequestMethod.GET)
    public ResponseEntity<ItemDesc> queryItemDesc(@PathVariable("id") Long id) {
        if (id == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        ItemDesc desc = this.itemDescService.queryById(id);
        if (desc == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(desc);
    }

    @RequestMapping(value = "cat/{id}",method = RequestMethod.GET)
    public ResponseEntity<ItemCat> queryItemCatById(@PathVariable("id") Long id) {
        if (id == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        ItemCat itemCat = this.itemCatService.queryById(id);
        if (itemCat == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(itemCat);
    }
}

